<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>xiaxuchen的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="Vue基础知识一. Vue实例1. 创建实例123456789101112&#x2F;&#x2F; 1. 通过这种方式可以将template中的内容挂在到id为root的节点上,传入的对象称为optionsnew Vue(&amp;#123;    &#x2F;&#x2F; 内容的挂载点    el: &amp;#x27;#root&amp;#x27;,    &#x2F;&#x2F; 渲染的内容    template: &amp;#x27;&lt;h1&gt;hello&lt;&#x2F;h1">
<meta property="og:type" content="article">
<meta property="og:title" content="xiaxuchen的博客">
<meta property="og:url" content="http://yoursite.com/2020/08/07/%E5%89%8D%E7%AB%AF/Vue%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/index.html">
<meta property="og:site_name" content="xiaxuchen的博客">
<meta property="og:description" content="Vue基础知识一. Vue实例1. 创建实例123456789101112&#x2F;&#x2F; 1. 通过这种方式可以将template中的内容挂在到id为root的节点上,传入的对象称为optionsnew Vue(&amp;#123;    &#x2F;&#x2F; 内容的挂载点    el: &amp;#x27;#root&amp;#x27;,    &#x2F;&#x2F; 渲染的内容    template: &amp;#x27;&lt;h1&gt;hello&lt;&#x2F;h1">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cn.vuejs.org/images/lifecycle.png">
<meta property="article:published_time" content="2020-08-07T15:08:02.769Z">
<meta property="article:modified_time" content="2020-07-19T10:36:53.935Z">
<meta property="article:author" content="xiaxuchen">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cn.vuejs.org/images/lifecycle.png">
  
    <link rel="alternate" href="./atom.xml" title="xiaxuchen的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="./css/style.css">

<meta name="generator" content="Hexo 5.0.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="./" id="logo">xiaxuchen的博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="./">Home</a>
        
          <a class="main-nav-link" href="./archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="./atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-前端/Vue基础知识" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="./2020/08/07/%E5%89%8D%E7%AB%AF/Vue%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" class="article-date">
  <time datetime="2020-08-07T15:08:02.769Z" itemprop="datePublished">2020-08-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="Vue基础知识"><a href="#Vue基础知识" class="headerlink" title="Vue基础知识"></a>Vue基础知识</h2><h3 id="一-Vue实例"><a href="#一-Vue实例" class="headerlink" title="一. Vue实例"></a>一. Vue实例</h3><h4 id="1-创建实例"><a href="#1-创建实例" class="headerlink" title="1. 创建实例"></a>1. 创建实例</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 通过这种方式可以将template中的内容挂在到id为root的节点上,传入的对象称为options</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    <span class="comment">// 内容的挂载点</span></span><br><span class="line">    el: <span class="string">&#x27;#root&#x27;</span>,</span><br><span class="line">    <span class="comment">// 渲染的内容</span></span><br><span class="line">    template: <span class="string">&#x27;&lt;h1&gt;hello&lt;/h1&gt;&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2. 不用el可以使用$mount方法,效果相同</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    template: <span class="string">&#x27;&lt;h1&gt;hello&lt;/h1&gt;&#x27;</span></span><br><span class="line">&#125;).$mount(<span class="string">&quot;#root&quot;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="2-data属性"><a href="#2-data属性" class="headerlink" title="2. data属性"></a>2. data属性</h4><p>用于声明数据，使得可以在模板中使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    <span class="comment">// 模板字符串渲染，将会显示hello</span></span><br><span class="line">    template: <span class="string">&#x27;&lt;h1&gt;&#123;&#123;text&#125;&#125;&lt;/h1&gt;&#x27;</span>,</span><br><span class="line">    data: &#123;</span><br><span class="line">        text: <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;).$mount(<span class="string">&quot;#root&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 可以通过以下方式操作data中的属性</span></span><br><span class="line">app.text++;</span><br></pre></td></tr></table></figure>

<h4 id="3-实例属性"><a href="#3-实例属性" class="headerlink" title="3. 实例属性"></a>3. 实例属性</h4><ul>
<li><p>vue.$data</p>
<p>创建vue时的data属性</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setInterval</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// app.text++;效果相同，都能更新数据</span></span><br><span class="line">    app.$data.text++;</span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>vue.$props</p>
<p>vue组件暴露的属性</p>
</li>
<li><p>vue.$el</p>
<p>挂载的节点</p>
</li>
<li><p>vue.$options</p>
<p>创建vue时传入的对象复制版，只可读，修改不会更新</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="comment">// 模板字符串渲染，将会显示hello</span></span><br><span class="line">    template: <span class="string">&#x27;&lt;h1&gt;&#123;&#123;text&#125;&#125;&lt;/h1&gt;&#x27;</span>,</span><br><span class="line">    data: &#123;</span><br><span class="line">        text: <span class="string">&#x27;hello&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">setInterval</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 无效</span></span><br><span class="line">    app.$options.text++;</span><br><span class="line">&#125;, <span class="number">1000</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>vue.$root</p>
<p>就是vue的实例，app</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue.$root &#x3D;&#x3D;&#x3D; app &#x2F;&#x2F; true</span><br></pre></td></tr></table></figure>
</li>
<li><p>vue.$children</p>
<p>可以拿到内容部分的子元素</p>
</li>
<li><p>vue.$refs</p>
<p>当元素上面有ref属性，我们可以通过vue.$refs.ref值引用dom元素</p>
</li>
<li><p>vue.$isServer</p>
<p>在服务器端渲染使用</p>
</li>
<li><p>vue.$watch</p>
<p>同写在options中效果一样，去监听属性的变化</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> unwatch = vue.$watch(<span class="string">&#x27;text&#x27;</span>,<span class="function">(<span class="params">old,<span class="keyword">new</span></span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 当text变化时调用</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 当销毁时,防止内存泄漏</span></span><br><span class="line">unwatch()</span><br></pre></td></tr></table></figure>
</li>
<li><p>vue.$emit</p>
<p>发出事件,同时可以传递数据</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue.$emit(<span class="string">&#x27;事件名称&#x27;</span>,...数据)</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>vue.$on</p>
<p>监听事件的发送，注意$on和$emit必须在同一个vue对象上</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vue.$on(&#39;事件名称&#39;,(...数据)&#x3D;&gt;&#123;</span><br><span class="line">    &#x2F;&#x2F; 当事件发送后触发</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>vue.$once</p>
<p>作用同$on但是只触发一次</p>
</li>
<li><p>vue.$forceUpdate</p>
<p>可以强制渲染，但是乱用会有性能问题</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">const app &#x3D; new Vue(&#123;</span><br><span class="line">    template: &#39;&lt;h1&gt;&#123;&#123;text&#125;&#125; &#123;&#123;obj.a&#125;&#125;&lt;&#x2F;h1&gt;&#39;,</span><br><span class="line">    data: &#123;</span><br><span class="line">        text: 0,</span><br><span class="line">        obj: &#123;&#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).$mount(&quot;#root&quot;)</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;当前obj并没有a属性，故不存在响应式，所以视图不会更新</span><br><span class="line">let i &#x3D; 0;</span><br><span class="line">setInterval(() &#x3D;&gt; &#123;</span><br><span class="line">    app.obj.a &#x3D; i;</span><br><span class="line">    &#x2F;&#x2F;app.$forceUpdate() 调用这个即可强制渲染</span><br><span class="line">    &#x2F;&#x2F;app.$set(app.obj,&#39;a&#39;,i) 这样也可以，不会有性能问题，就像直接定义</span><br><span class="line">&#125;, 1000)</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>vue.$set</p>
<p>当data中的对象要添加属性时可以使用此方法，将其加入响应式</p>
</li>
<li><p>vue.$delete</p>
<p>当data中的对象或者属性不需要了，可以使用$delete移除，以免内存泄漏</p>
</li>
<li><p>vue.$nextTicket</p>
<p>vue的视图更新不是数据一变化就更新的，很多时候连续的数据变化可能只会更新一次，这样的话我们可以使用$nextTicket在下次更新的时候更新数据</p>
</li>
</ul>
<h3 id="二、-Vue的生命周期"><a href="#二、-Vue的生命周期" class="headerlink" title="二、 Vue的生命周期"></a>二、 Vue的生命周期</h3><p><img src="https://cn.vuejs.org/images/lifecycle.png" alt="Vue 实例生命周期"></p>
<h3 id="三、Vue数据绑定难点"><a href="#三、Vue数据绑定难点" class="headerlink" title="三、Vue数据绑定难点"></a>三、Vue数据绑定难点</h3><ul>
<li><p>computed</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">computed: &#123;</span><br><span class="line">    name: &#123;</span><br><span class="line">        get () &#123;</span><br><span class="line">            return this.firstName + this.LastName</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F; 可以设置值，但不建议</span><br><span class="line">        set (nameVal) &#123;</span><br><span class="line">            let names &#x3D; nameVal.split(&#39; &#39;)</span><br><span class="line">            this.firstName &#x3D; names[0]</span><br><span class="line">            this.lastName &#x3D; names[1]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#x2F;&#x2F; 一般直接这样，只获取不设置</span><br><span class="line">    computedValue () &#123;</span><br><span class="line">        return this.value + 1</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>watch</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">watch: &#123;</span><br><span class="line">        text: &#123;</span><br><span class="line">            handler(oldVal, newVal) &#123;</span><br><span class="line">                <span class="comment">// 监听到数据变化则会触发，不适用于数据的显示，更适用于请求服务器</span></span><br><span class="line">            &#125;,</span><br><span class="line">            <span class="comment">// 绑定时也执行一次</span></span><br><span class="line">            immediate: <span class="literal">true</span>,</span><br><span class="line">            <span class="comment">// 对象的属性变化也能触发，但是性能开销大，如果只对一个属性监听可以直接监听那个属性</span></span><br><span class="line">            deep: <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h3 id="四、组件"><a href="#四、组件" class="headerlink" title="四、组件"></a>四、组件</h3><ul>
<li><p>props</p>
<p>props最好不要在组件内部修改，props的格式，prop属性可以自定义验证格式如下:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">props: &#123;</span><br><span class="line">       name: &#123;</span><br><span class="line">           validator(value) &#123;</span><br><span class="line">            <span class="comment">// 校验逻辑</span></span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<ul>
<li><p>Vue.extend</p>
<p>通过他可以复用options，一般可以用做完成通用的功能</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> Comp = Vue.extend(options)</span><br><span class="line"><span class="keyword">new</span> Comp(newOptions) <span class="comment">//这样就可以合并两个options的内容，起到一个复用的功能</span></span><br><span class="line"><span class="comment">// 当在实例中使用</span></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">    <span class="keyword">extends</span>: options</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/08/07/%E5%89%8D%E7%AB%AF/Vue%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" data-id="ckdkdagty0008vout8tab2da1" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="./2020/08/07/%E5%89%8D%E7%AB%AF/vue%E8%87%AA%E5%AE%9A%E4%B9%89%E7%BB%84%E4%BB%B6src%E6%97%A0%E6%95%88/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="./2020/08/07/%E5%89%8D%E7%AB%AF/vue%20%E7%BB%84%E4%BB%B6/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="./archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E9%93%BE%E8%A1%A8/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%A0%88%E4%B8%8E%E9%98%9F%E5%88%97/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B9%8B%E6%95%B0%E7%BB%84/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E6%80%9D%E7%BB%B4%E5%AF%BC%E5%9B%BE/">(no title)</a>
          </li>
        
          <li>
            <a href="./2020/08/07/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/SpringBoot%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 xiaxuchen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="./" class="mobile-nav-link">Home</a>
  
    <a href="./archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="./fancybox/jquery.fancybox.css">

  
<script src="./fancybox/jquery.fancybox.pack.js"></script>




<script src="./js/script.js"></script>




  </div>
</body>
</html>